<?php

namespace CommonApi\Controller;

use Common\Model\ImageModel;
use Common\Model\PatientTestModel;
use Think\Controller;
//use Common\RestController;
use Common\CommonController;
use Common\Model\CarTurnoverModel;
use Common\Model\PatientModel;
use Common\Model\PatientLifeStatusModel;
use Common\Model\PatientNormalCureModel;
use Common\Model\CaseLogicModel;
use Common\Model\PatientMedicineCureModel;

class PatientController extends CommonController {


    private $carTurnoverModel;
    private $patientModel;

    public function __construct() {
        parent::__construct();

        $this->carTurnoverModel = new CarTurnoverModel;
        $this->patientModel = new PatientModel;
    }


    /**
     * 获取病人信息
     * @type post
     * @param token , patient_id
     */
    public function getPatientInfo() {
        $patient_id = I('patient_id');
        if (empty($patient_id)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '病人id必须填写']);
        }

        $info = $this->patientModel->getPatientInfo(I('patient_id'));
        if (empty($info)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '该病人不存在']);
        }

        //返回病人信息
        $this->ajaxReturn(['status' => 1, 'data' => $info]);
    }

    public function getPatientNormalCure(){
        $patient_id = I('patient_id');
        if (empty($patient_id)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '病人id必须填写']);
        }

        $normalM = new PatientNormalCureModel();
        $info = $normalM->getAllInfo($patient_id);
        $this->ajaxReturn(['status'=>1,'data'=>$info]);
    }

    public function getPatientMedicineCure(){
        $patient_id = I('patient_id');
        if (empty($patient_id)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '病人id必须填写']);
        }

        $normalM = new PatientMedicineCureModel();
        $info = $normalM->getAllInfo($patient_id);
        $this->ajaxReturn(['status'=>1,'data'=>$info]);
    }


    /**
     * 修改病人信息
     * patient_id,
     * patient_info
     */
    public function editPatient() {
        $patient_id = I('patient_id');
        if (empty($patient_id)) {
            $this->ajaxReturn(['status' => 0, '接口传参错误，需要传patient_id']);
        }

        if (!$this->patientModel->create()) {
            $this->ajaxReturn(['status' => 0, 'error_info' => $this->patientModel->getError()]);
        }

        $this->patientModel->where(['id' => $patient_id])->save();
        $this->ajaxReturn(['status' => 1]);
    }

    /**
     * hospital端提交交接信息
     * token
     * car_id ,patient_id
     * signature 120用户签名
     */
    public function closeCase() {
        $caseLogicModel = new CaseLogicModel;
        $result = $caseLogicModel->closeCaseForHospital(I('patient_id'), I('signature'));

        $this->ajaxReturn($result);
    }

    /**
     * token
     * patient_id
     */
    public function getLastlifeStatus() {
        $lifeModel = new PatientLifeStatusModel;
        $record = $lifeModel->where(['patient_id' => I('patient_id')])->order('id desc')->limit(1)->select();
        if (empty($record)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '没有用户生命体征记录']);
        } else {
            $this->ajaxReturn(['status' => 1, 'data' => ['record' => $record]]);
        }
    }

    /**
     * token
     * patient_id
     * offset
     * length
     * all: 1 or 0
     */
    public function getlifestatus() {
        $all = I('all');
        $patient_id = I('patient_id');
        $lifeModel = new PatientLifeStatusModel;

        if ($all) {
            //all records
            $records = $lifeModel->getPatientAllRecords($patient_id);
        } else {
            //customer records
            $records = $lifeModel->getPatientRecords($patient_id, I('offset'), I("length"));
        }

        $count = $lifeModel->getPatientRecordsCount($patient_id);

        if (empty($records)) {
            $this->ajaxReturn(['status' => 0, 'error_info' => '没有用户生命体征记录']);
        } else {
            $this->ajaxReturn(['status' => 1, 'data' => ['record' => $records, 'count' => $count]]);
        }
    }

    /**
     * token
     * patient_id
     * test_name iss / gcs / rems
     * test_result {total:10,detail:[{id:1,answer:1,score:1},{id:2,answer:2,score:2}]}
     */
    public function addTestResult(){

        $testM = new PatientTestModel();

        if(!$testM->create()){
            $this->ajaxReturn(['status'=>0,'error_info'=>$testM->getError()]);
        }

        //删除旧数据
        (new PatientTestModel())->where(['patient_id'=>I('patient_id'),'test_name'=>I('test_name')])->delete();

        if($testM->add()){
            $this->ajaxReturn(['status'=>1]);
        }else{
            $this->ajaxReturn(['status'=>0,'error_info'=>$testM->getError()]);
        }

    }

    public function addAvatar(){

        $user_id= I('post.patient_id');

        $image = new ImageModel();
        $image=$image->uploadImage($user_id,$_FILES['image']);
        $this->ajaxReturn(['status'=>1,'image'=>$image]);

    }



    /**
     * token
     * patient_id
     * test_name
     */
    public function getTestResult(){
        $patient_id = I('patient_id');
        $test_name = I('test_name');

        if(empty($patient_id)||empty($test_name)){
            $this->ajaxReturn(['status'=>0,'error_info'=>'参数错误']);
        }

        $testM = new PatientTestModel();
        $info = $testM->where(['patient_id'=>$patient_id,'test_name'=>$test_name])->find();
        if($info == null){
            $this->ajaxReturn(['status'=>0,'error_info'=>'没有该用户的测试数据']);
        }else{

            $this->ajaxReturn(['status'=>1,'data'=>['raw'=>$info['test_result'],'detail'=>json_decode($info['test_result'],true)] ]);
        }
    }



}